package com.resou.reader.paycenter.m;

import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.PayConfigBean;
import com.resou.reader.bookshelf.listener.OnItemClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by ${Garrett} on 2018/10/16.
 * Contact me krouky@outlook.com
 */
public class PayConfigAdapter extends RecyclerView.Adapter<PayConfigAdapter.ViewHolder> {

    private List<PayConfigBean> mList;
    private OnItemClickListener onItemClickListener;

    public PayConfigAdapter(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<PayConfigBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_pay_config, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        PayConfigBean bean = mList.get(i);
        viewHolder.tvMoney.setText(String.valueOf((int) bean.getCnyPrice() + viewHolder.itemView.getResources().getString(R.string.unit_yuan)));
        viewHolder.tvCurrency.setText(String.valueOf((int) bean.getBookBean() + viewHolder.itemView.getResources().getString(R.string.currency)));

        int giftBean = (int) bean.getGiftBean();
        if (giftBean > 0) {
            String voucherStr = String.valueOf("送 " + giftBean + viewHolder.itemView.getResources().getString(R.string.voucher));
            viewHolder.tvCurrency.setText(voucherStr);
        } else {
            viewHolder.tvCurrency.setText("");
        }
        if (bean.getSuperscript() == 1) {//首充角标
            viewHolder.icCornerMark.setVisibility(View.VISIBLE);
            viewHolder.icCornerMark.setImageResource(R.mipmap.first_charge_mark);
        } else {
            viewHolder.icCornerMark.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_currency)
        TextView tvCurrency;
        @BindView(R.id.tv_voucher)
        TextView tvVoucher;
        @BindView(R.id.ic_corner_mark)
        ImageView icCornerMark;
        @BindView(R.id.pay_config_container)
        ConstraintLayout payConfigContainer;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClicked(PayConfigAdapter.this, v, getAdapterPosition());
                }
            });
        }
    }


}
